﻿using System;
using GeoAPI.Geometries;
using NetTopologySuite.Algorithm;

namespace NetTopologySuite.Noding
{
	public class IntersectionAdder : ISegmentIntersector
	{
		public static bool IsAdjacentSegments(int i1, int i2)
		{
			return System.Math.Abs(i1 - i2) == 1;
		}

		public IntersectionAdder(LineIntersector li)
		{
			this._li = li;
		}

		public LineIntersector LineIntersector
		{
			get
			{
				return this._li;
			}
		}

		public Coordinate ProperIntersectionPoint
		{
			get
			{
				return this._properIntersectionPoint;
			}
		}

		public bool HasIntersection
		{
			get
			{
				return this._hasIntersection;
			}
		}

		public bool HasProperIntersection
		{
			get
			{
				return this._hasProper;
			}
		}

		public bool HasProperInteriorIntersection
		{
			get
			{
				return this._hasProperInterior;
			}
		}

		public bool HasInteriorIntersection
		{
			get
			{
				return this._hasInterior;
			}
		}

		private bool IsTrivialIntersection(ISegmentString e0, int segIndex0, ISegmentString e1, int segIndex1)
		{
			if (e0 == e1 && this._li.IntersectionNum == 1)
			{
				if (IntersectionAdder.IsAdjacentSegments(segIndex0, segIndex1))
				{
					return true;
				}
				if (e0.IsClosed)
				{
					int num = e0.Count - 1;
					if ((segIndex0 == 0 && segIndex1 == num) || (segIndex1 == 0 && segIndex0 == num))
					{
						return true;
					}
				}
			}
			return false;
		}

		public void ProcessIntersections(ISegmentString e0, int segIndex0, ISegmentString e1, int segIndex1)
		{
			if (e0 == e1 && segIndex0 == segIndex1)
			{
				return;
			}
			this.NumTests++;
			Coordinate[] coordinates = e0.Coordinates;
			Coordinate p = coordinates[segIndex0];
			Coordinate p2 = coordinates[segIndex0 + 1];
			Coordinate[] coordinates2 = e1.Coordinates;
			Coordinate p3 = coordinates2[segIndex1];
			Coordinate p4 = coordinates2[segIndex1 + 1];
			this._li.ComputeIntersection(p, p2, p3, p4);
			if (this._li.HasIntersection)
			{
				this.NumIntersections++;
				if (this._li.IsInteriorIntersection())
				{
					this.NumInteriorIntersections++;
					this._hasInterior = true;
				}
				if (!this.IsTrivialIntersection(e0, segIndex0, e1, segIndex1))
				{
					this._hasIntersection = true;
					((NodedSegmentString)e0).AddIntersections(this._li, segIndex0, 0);
					((NodedSegmentString)e1).AddIntersections(this._li, segIndex1, 1);
					if (this._li.IsProper)
					{
						this.NumProperIntersections++;
						this._hasProper = true;
						this._hasProperInterior = true;
					}
				}
			}
		}

		public bool IsDone
		{
			get
			{
				return false;
			}
		}

		private bool _hasIntersection;

		private bool _hasProper;

		private bool _hasProperInterior;

		private bool _hasInterior;

		private readonly Coordinate _properIntersectionPoint;

		private readonly LineIntersector _li;

		public int NumIntersections;

		public int NumInteriorIntersections;

		public int NumProperIntersections;

		public int NumTests;
	}
}
